package org.pentaho.di.ui.core.widget;

import com.google.common.base.Preconditions;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.pentaho.di.trans.TransMeta;
import org.pentaho.di.ui.core.FormDataBuilder;
import org.pentaho.di.ui.core.PropsUI;

public class AuthComposite extends Composite {
    private final PropsUI props;
    private final ModifyListener lsMod;
    private final TransMeta transMeta;
    private String authGroupLabel;
    private Group wAuthGroup;
    private String usernameLabel;
    private Label wlUsername;
    private TextVar wUsername;
    private String passwordLabel;
    private Label wlPassword;
    private TextVar wPassword;

    public AuthComposite(Composite composite, int style, PropsUI props, ModifyListener lsMod, TransMeta transMeta, String credentialGroupName, String usernameLabel, String passwordLabel) {
        super(composite, style);
        Preconditions.checkNotNull(props);
        Preconditions.checkNotNull(lsMod);
        Preconditions.checkNotNull(transMeta);
        this.props = props;
        this.lsMod = lsMod;
        this.transMeta = transMeta;
        this.authGroupLabel = credentialGroupName;
        this.usernameLabel = usernameLabel;
        this.passwordLabel = passwordLabel;
        this.layoutComposite();
    }

    private void layoutComposite() {
        FormLayout flAuthComp = new FormLayout();
        this.setLayout(flAuthComp);
        this.wAuthGroup = new Group(this, 16);
        this.props.setLook(this.wAuthGroup);
        this.wAuthGroup.setText(this.authGroupLabel);
        FormLayout flAuthGroup = new FormLayout();
        flAuthGroup.marginHeight = 15;
        flAuthGroup.marginWidth = 15;
        this.wAuthGroup.setLayout(flAuthGroup);
        this.wAuthGroup.setLayoutData((new FormDataBuilder()).fullSize().result());
        this.wlUsername = new Label(this.wAuthGroup, 16384);
        this.props.setLook(this.wlUsername);
        this.wlUsername.setText(this.usernameLabel);
        this.wlUsername.setLayoutData((new FormDataBuilder()).left().top().result());
        this.wUsername = new TextVar(this.transMeta, this.wAuthGroup, 18436);
        this.props.setLook(this.wUsername);
        this.wUsername.addModifyListener(this.lsMod);
        this.wUsername.setLayoutData((new FormDataBuilder()).top(this.wlUsername).left().width(350).result());
        this.wlPassword = new Label(this.wAuthGroup, 16384);
        this.props.setLook(this.wlPassword);
        this.wlPassword.setText(this.passwordLabel);
        this.wlPassword.setLayoutData((new FormDataBuilder()).left().top(this.wUsername, 10).result());
        this.wPassword = new PasswordTextVar(this.transMeta, this.wAuthGroup, 18436);
        this.props.setLook(this.wPassword);
        this.wPassword.addModifyListener(this.lsMod);
        this.wPassword.setLayoutData((new FormDataBuilder()).left().top(this.wlPassword).width(350).result());
    }

    public String getUsername() {
        return this.wUsername.getText();
    }

    public String getPassword() {
        return this.wPassword.getText();
    }

    public void setUsername(String username) {
        this.wUsername.setText(username);
    }

    public void setPassword(String password) {
        this.wPassword.setText(password);
    }

    public void setVisible(boolean visible) {
        super.setVisible(visible);
        this.wAuthGroup.setVisible(visible);
        this.wlUsername.setVisible(visible);
        this.wUsername.setVisible(visible);
        this.wlPassword.setVisible(visible);
        this.wPassword.setVisible(visible);
    }
}

